import axios from "axios";
import EventBus from './event'

const instance = axios.create({
    timeout: 5000,
    withCredentials: true
});

instance.interceptors.request.use(
    config => {
        return config
    },
    error => {
        console.log('传参错误')
        return Promise.reject(error)
    }
)

// 请求结果守卫拦截
instance.interceptors.response.use(function (response) {
    if (response.status === 200) {
        if(response.data.code === 401) {
            EventBus.emit('global_not_login', response.data.msg)
            return Promise.reject('没有登录状态')
        }
        // 全局的错误处理
        if (response.data.code !== 0 && response.data.code !== 401) {
            EventBus.emit('global_error_tips', response.data.msg)
        }
    } else {
        EventBus.emit('global_error_tips', response.data.message)
    }
    return response;
}, function (error) {
    EventBus.emit('global_error_tips', error.response.data.message)
    return Promise.reject(error);
});

export default instance